﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using TotalCrytography.Protector;

namespace TotalCrytography
{
    public partial class SignGUI : Form
    {
        private List<String> m_lstFiles = new List<string>();
        public SignGUI()
        {
            InitializeComponent();
        }

        public SignGUI(List<String> lstSelectedFiles)
        {
            InitializeComponent();
            txtPath.Text = lstSelectedFiles[0].Substring(0, lstSelectedFiles[0].LastIndexOf('\\') + 1);
            for (int i = 0; i < lstSelectedFiles.Count; i++)
            {
                if (File.Exists(lstSelectedFiles[i]))
                {
                    lstFiles.Items.Add(new ListViewItem(lstSelectedFiles[i]));
                    m_lstFiles.Add(lstSelectedFiles[i]);
                }
            }
        }

        private void ShowList()
        {
            lstFiles.Items.Clear();
            for (int i = 0; i < m_lstFiles.Count; i++)
            {
                if (File.Exists(m_lstFiles[i]))
                    lstFiles.Items.Add(new ListViewItem(m_lstFiles[i]));
            }
        }

        private void btnExit_Click(object sender, EventArgs e)
        {
            Close();
        }

        private void btnBrowse_Click(object sender, EventArgs e)
        {
            FolderBrowserDialog fd = new FolderBrowserDialog();
            if (fd.ShowDialog() == DialogResult.OK)
                txtPath.Text = fd.SelectedPath + "\\";
        }

        private void btnDelete_Click(object sender, EventArgs e)
        {
            if (MessageBox.Show("Do you want to remove these files?", "Delete", MessageBoxButtons.OKCancel, MessageBoxIcon.Question, MessageBoxDefaultButton.Button1) == DialogResult.OK)
            {
                foreach (ListViewItem item in lstFiles.SelectedItems)
                {
                    m_lstFiles.Remove(item.Text);
                }
                ShowList();
            }
        }

        private void btnSign_Click(object sender, EventArgs e)
        {
            List<String> lstSelectFile = m_lstFiles;
            Boolean successful = true;
            for (int i = 0; i < lstSelectFile.Count; i++)
            {
                String inputFile = lstSelectFile[i];
                String outputFile = txtPath.Text + lstSelectFile[i].Substring(lstSelectFile[i].LastIndexOf('\\') + 1) + ".sig";
                try
                {
                    //eSignature.createSignature(inputFile, outputFile);
                    FileStream fsread = new FileStream(inputFile, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
                    StreamReader sr = new StreamReader(fsread);
                    StreamWriter sw = new StreamWriter(outputFile);
                    string strInput = sr.ReadToEnd();
                    // decrypt to get private key
                    Symmetric_Provider symProvider = new Symmetric_Provider(MainGUI.GetCurrentPass());
                    string privateKey = symProvider.DecryptString(MainGUI.GetCurrentUser().PrivateKey);
                    string strOutput = eSignature.createSignature(strInput, "SHA256", privateKey);
                    sw.Write(strOutput);
                    sr.Close();
                    fsread.Close();
                    sw.Close();
                    if (strOutput == "")
                    {
                        MessageBox.Show("Fail to create signature for " + inputFile + "!", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error, MessageBoxDefaultButton.Button1);
                        DialogResult = DialogResult.OK;
                        successful = false;
                    }
                }
                catch (System.Exception ex)
                {
                    MessageBox.Show("Fail to create signature for " + inputFile + "!", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error, MessageBoxDefaultButton.Button1);
                    successful = false;
                    continue;
                }
            }
            if (successful)
                MessageBox.Show("Create sign successfully!", "TotalCrytography", MessageBoxButtons.OK, MessageBoxIcon.Information, MessageBoxDefaultButton.Button1);
            DialogResult = DialogResult.OK;
            Close();
        }

        private void lstFiles_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

        

        
    }
}
